package com.hopu.v1.service.admin.exam.impl;

import com.aliyun.oss.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hopu.v1.mapper.admin.exam.ExamOptionAnswerMapper;
import com.hopu.v1.mapper.admin.exam.ExamQuestionsMapper;
import com.hopu.v1.pojo.exam.ExamOptionAnswer;
import com.hopu.v1.pojo.exam.ExamQuestions;
import com.hopu.v1.service.admin.exam.ExamQuestionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author lihao
* @description 针对表【t_exam_questions(考试题目表，存储与考试相关的题目信息)】的数据库操作Service实现
* @createDate 2024-09-25 11:24:44
*/
@Service
public class ExamQuestionsServiceImpl extends ServiceImpl<ExamQuestionsMapper, ExamQuestions>
    implements ExamQuestionsService {
    @Autowired
    private ExamOptionAnswerMapper examOptionAnswerMapper;

    @Autowired
    private ExamQuestionsMapper examQuestionsMapper;


    @Transactional
    @Override
    public void add(ExamQuestions examQuestions) {
        examQuestions.setCreateTime(LocalDateTime.now());
        //新增考题题目
        save(examQuestions);
        examQuestions.getOptions().stream().forEach(option -> {option.setQuestionId(examQuestions.getId());});
        //新增考题选项
        examOptionAnswerMapper.insert(examQuestions.getOptions());
    }

    @Override
    public void batchAdd(List<ExamQuestions> examQuestionsList) {
        examQuestionsMapper.insert(examQuestionsList);
    }

    @Transactional
    @Override
    public void del(Integer id) {
        examQuestionsMapper.deleteById(id);
        LambdaQueryWrapper<ExamOptionAnswer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamOptionAnswer::getQuestionId, id);

        List<Integer> ids = examOptionAnswerMapper.selectList(queryWrapper)
                .stream()
                .map(ExamOptionAnswer::getId)
                .collect(Collectors.toList());

        examOptionAnswerMapper.deleteBatchIds(ids);

    }

    @Transactional
    @Override
    public void update(ExamQuestions examQuestions) {
        examQuestions.setUpdateTime(LocalDateTime.now());
        examQuestionsMapper.updateById(examQuestions);
        examOptionAnswerMapper.updateById(examQuestions.getOptions());
    }

    @Override
    public IPage<ExamQuestions> selectList(Page<ExamQuestions> page, ExamQuestions examQuestions,String endTime) {
        LambdaQueryWrapper<ExamQuestions> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isNullOrEmpty(examQuestions.getQuestionTitle())) {
            wrapper.like(ExamQuestions::getQuestionTitle, examQuestions.getQuestionTitle());
        } else if (examQuestions.getCreateTime() != null) {
            wrapper.between(ExamQuestions::getCreateTime, examQuestions.getCreateTime(),endTime);
        }
        wrapper.orderByDesc(ExamQuestions::getCreateTime);
        return examQuestionsMapper.selectPage(page, wrapper);
    }

}




